package com.hxd.otc.module.my.presenter;

import android.support.annotation.NonNull;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.common.enums.IdCardStatusEnum;
import com.hxd.otc.common.enums.RoleTypeEnum;
import com.hxd.otc.module.my.activity.RealNameStep1Activity;
import com.hxd.otc.module.my.bean.ApplyExplainContentBean;
import com.hxd.otc.module.my.bean.ApplyParamBean;
import com.hxd.otc.module.my.bean.UploadImageBean;
import com.hxd.otc.module.my.bean.UserStatusBean;
import com.hxd.otc.module.my.bean.YsPreBean;
import com.hxd.otc.module.my.contract.ApplyContract;
import com.hxd.otc.module.my.model.BondModel;
import com.hxd.otc.module.my.model.UserModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.utils.ResourceUtil;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * Created by lzy on 2018/6/19 0019.
 * description ${申请广告商 Presenter}
 */
public class ApplyPresenter extends BasePresenter<ApplyContract.View> implements ApplyContract.Presenter {


    private final BondModel mBondModel;
    private final UserModel mUserModel;
    private HashMap<String, UploadImageBean> mImageBeanHashMap;
    private YsPreBean mYsPreBean;

    public ApplyPresenter(ApplyContract.View view) {
        super(view);
        mBondModel = BondModel.getInstance();
        mUserModel = UserModel.getInstance();
    }

    @Override
    public void apply(ApplyParamBean item, RoleTypeEnum roleTypeEnum) {

        if (mImageBeanHashMap == null) {
            mView.showToast(ResourceUtil.getString(R.string.please_upload_the_authentication_picture));
            mView.applyFail("", false);
            return;
        }

        String cardUpId = mImageBeanHashMap.get(RealNameStep1Activity.IMAGE_TAG_FACE).getRemotePath();
        String cardDownId = mImageBeanHashMap.get(RealNameStep1Activity.IMAGE_TAG_BACK).getRemotePath();
        String cardFaceId = mImageBeanHashMap.get(RealNameStep1Activity.IMAGE_TAG_HOLD).getRemotePath();

        mBondModel.apply(item.getCronKey(), cardUpId, cardDownId, cardFaceId, roleTypeEnum, new BaseObserver<Object>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull Object o) {
                mView.applySuccess(msg);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.applyFail(errMessage, true);
            }

        }, mView.<BaseBean<Object>>bindAutoDispose());
    }

    @Override
    public void findCardStatus() {
        BaseObserver<UserStatusBean> baseObserver = new BaseObserver<UserStatusBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull UserStatusBean userStatusBean) {
                if (userStatusBean == null) return;
                UserHelper.updateCardStatus(IdCardStatusEnum.getIDCardStatusEnum(userStatusBean.getCardStatus()));
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                if (BaseObserver.TOKEN_MISS == errType) {//登录失效
                    mView.loginMiss();
                }
            }
        };
        baseObserver.setPostTokenMissEvent(false);
        mUserModel.findCardStatus(baseObserver, mView.<BaseBean<UserStatusBean>>bindAutoDispose());
    }

    @Override
    public void requestYsBond() {
        mBondModel.requestYsBond(new BaseObserver<YsPreBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull YsPreBean ysPreBean) {
                mYsPreBean = ysPreBean;
                mView.updateBond();
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<YsPreBean>>bindAutoDispose());
    }

    @Override
    public ArrayList<ApplyExplainContentBean> getExplainContent(RoleTypeEnum roleTypeEnum) {
        ArrayList<ApplyExplainContentBean> lists = new ArrayList<>();
        if (RoleTypeEnum.NonInductiveSliver.equals(roleTypeEnum)) {//无感支付广告商
            lists.add(new ApplyExplainContentBean(R.string.apply_non_inductive_pay_silver_item_title_1, R.string.apply_non_inductive_pay_silver_item_desc_1, R.mipmap.icon_apply_silver_logo_1));
            lists.add(new ApplyExplainContentBean(R.string.apply_non_inductive_pay_silver_item_title_2, R.string.apply_non_inductive_pay_silver_item_desc_2, R.mipmap.icon_apply_silver_logo_2));
            lists.add(new ApplyExplainContentBean(R.string.apply_non_inductive_pay_silver_item_title_3, R.string.apply_non_inductive_pay_silver_item_desc_3, R.mipmap.icon_apply_silver_logo_3));
        } else {//普通广告商
            lists.add(new ApplyExplainContentBean(R.string.apply_silver_item_title_1, R.string.apply_silver_item_desc_1, R.mipmap.icon_apply_silver_logo_1));
            lists.add(new ApplyExplainContentBean(R.string.apply_silver_item_title_2, R.string.apply_silver_item_desc_2, R.mipmap.icon_apply_silver_logo_2));
            lists.add(new ApplyExplainContentBean(R.string.apply_silver_item_title_3, R.string.apply_silver_item_desc_3, R.mipmap.icon_apply_silver_logo_3));
        }
        return lists;
    }

    @Override
    public void updateYsPreBean(YsPreBean ysPreBean) {
        mYsPreBean = ysPreBean;
        if (mYsPreBean == null) {
            requestYsBond();
        } else {
            mView.updateBond();
        }
    }

    @Override
    public ArrayList<ApplyParamBean> getBonds(RoleTypeEnum sliverType) {
        if (mYsPreBean == null) {
            requestYsBond();
            return null;
        }
        return RoleTypeEnum.NonInductiveSliver.equals(sliverType) ? mYsPreBean.getNoSenseBonds() : mYsPreBean.getParams();
    }

    @Override
    public void updateImageMap(HashMap<String, UploadImageBean> imageBeanHashMap) {
        mImageBeanHashMap = imageBeanHashMap;
    }
}
